package com.jsgt.greekfire.ui.pool.transfercoin.fragment

import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.fhx.core.base.BaseFragment
import com.fhx.core.utils.ToastUtils
import com.jsgt.greekfire.R
import com.jsgt.greekfire.common.util.MD5Util
import com.jsgt.greekfire.factory.CoinViewModelFactory
import com.jsgt.greekfire.viewmodel.CoinViewModel
import kotlinx.android.synthetic.main.frgamnet_transfer_out.*
import kotlinx.android.synthetic.main.frgamnt_change_into.*
import kotlinx.android.synthetic.main.frgamnt_change_into.openpool_text2
import kotlinx.android.synthetic.main.frgamnt_change_into.transfer_sure
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import kotlinx.android.synthetic.main.frgamnet_transfer_out.openpool_text1 as openpool_text11
import kotlinx.android.synthetic.main.frgamnet_transfer_out.transfer_password as transfer_password1
import kotlinx.android.synthetic.main.frgamnt_change_into.transfer_number as transfer_number1

class ChangeIntoFragment : BaseFragment() {

    private val coinViewModel: CoinViewModel by lazy {
        ViewModelProvider(
            this,
            CoinViewModelFactory()
        ).get(CoinViewModel::class.java)
    }

    private var changName: String? = null
    override fun getLayoutId(): Int = R.layout.frgamnt_change_into


    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // 注册，订阅
        EventBus.getDefault().register(this)
        return super.onCreateView(inflater, container, savedInstanceState)
    }


    // 接收黏性事件的方法
//    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
//    fun getStickyData(i: Int) {
//        if (i == 0) {
//            openpool_text1.text = "矿池"
//            openpool_text2.text = "GFC"
//        } else {
//            openpool_text1.text = "矿池"
//            openpool_text2.text = "GFT"
//
//        }
//    }


    override fun main() {
        transfer_sure.setOnClickListener {
            hideSoftKeyboard(activity)
            if (TextUtils.isEmpty(transfer_number.text.toString().trim())) {
                ToastUtils.show(activity!!, "数量不能为空")
                transfer_sure.isClickable = true
                return@setOnClickListener
            }

            if (TextUtils.isEmpty(transfer_password.text.toString().trim())) {
                ToastUtils.show(activity!!, "请输入交易密码")
                transfer_sure.isClickable = true
                return@setOnClickListener
            }

            showProgress(true)
            coinViewModel.poolOut(
                transfer_number.text.toString().trim(),
                MD5Util.getMD5(transfer_password.text.toString().trim())
            )
            transfer_sure.isClickable = false
        }

        coinViewModel.convertLiveData.observe(this, Observer {
            showProgress(false)
            transfer_sure.isClickable = true
            activity?.finish()
        })

        coinViewModel.errorLiveData.observe(this, Observer {
            showProgress(false)
            transfer_sure.isClickable = true
        })
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }

}